package com.wang.mapreduce2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * @Author: wang
 * @Date: 2020/1/26 21:50
 * @Description:
 */
public class Fruit2Driver implements Tool {

    private Configuration configuration = null;


    @Override
    public int run(String[] strings) throws Exception {
        // 1.获取Job实例
        Job job = Job.getInstance(configuration);
        // 2.指定本程序的jar包所在的本地路径
        job.setJarByClass(Fruit2Driver.class);
        // 3.设置mapper和输出的KV类型
        TableMapReduceUtil.initTableMapperJob("fruit",
                new Scan(),
                Fruit2Mapper.class,
                ImmutableBytesWritable.class,
                Put.class,
                job);
        // 4.设置reducer和输出的表
        TableMapReduceUtil.initTableReducerJob("fruit3", Fruit2Reducer.class, job);
        // 5.将job中配置的相关参数，以及job所用的java类所在的jar包，提交给yarn去运行
        boolean result = job.waitForCompletion(true);
        return result ? 0 : 1;
    }

    @Override
    public void setConf(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public Configuration getConf() {
        return configuration;
    }

    public static void main(String[] args) {
        try {
            Configuration configuration = HBaseConfiguration.create();
            ToolRunner.run(configuration, new Fruit2Driver(), args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
